let app = getApp()
const db = wx.cloud.database({
  env: app.globalData.CLOUD_ENV
})
const DBClassify = db.collection('db_classifys')
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    ctype: {
      type: String,
      value: ''
    }
  },
  
  // 监听器
  observers: {
    ctype:function(value){
      if(value){
        // console.log('x:',value)
        let key = "_"+value;
        let _start = "_"+value+"_time_";
        this.setData({key, _start})
        let str = wx.getStorageSync(key)
        let _start_time = wx.getStorageSync(_start)
        if(str){
          if(_start_time){
            let _end_time = new Date().getTime();
            if((_end_time - _start_time) > 4 * 60 * 60 * 1000){
              this.listClassifyFn2(value);
            }
          }
          let classifys = JSON.parse(str)
          this.setData({classifys})
        }
        else {
          this.listClassifyFn2(value);
        }
      }
    }
  },

  lifetimes: {
    attached() {
      //  初始化
      // this.listClassifyFn();
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    classifys: [],
    num: 0,
    direction: 'y', //x
    key: '',
    start_time: 0
  },

  /**
   * 组件的方法列表
   */
  methods: {
    showListFn() {
      let direction = this.data.direction;
      if (direction == 'y') {
        direction = 'x';
      } else {
        direction = 'y';
      }
      this.setData({
        direction
      })
    },

    async listClassifyFn2(type) {
      let that = this;
      await DBClassify.where({type}).get()
        .then(
          content => {
            // console.log('3.0', content)
            let key = that.data.key;
            let start_time = that.data.start_time;

            if (content.data) {
              that.setData({
                classifys: content.data
              })
              wx.setStorageSync(key, JSON.stringify(content.data))
              wx.setStorageSync(start_time, new Date().getTime())
          
            }
          }
        )
    },
  }
})